import api from '../../api';
import { message } from 'antd';
import moment from 'moment';

export default {
  namespace: 'moneyInformationM',
  state: {},
  effects: {
    // 查询理财直融信息
    *queryMoneyInformation({ payload, callback }, { call, put }) {
      const response = yield call(api.moneyInformation.queryMoneyInformation, payload);
      if (response && response.code == 0) {
        callback(response.data);
      } else {
        message.error(`查询列表失败，失败原因：${response?.msg}`);
        callback();
      }
    },

    // 新增编辑保存接口
    *addMoneyInformation({ payload, callback }, { call, put }) {
      const response = yield call(api.moneyInformation.addMoneyInformation, payload);
      if (response && response.success) {
        message.success('保存成功');
        callback(true);
      } else {
        message.error(`保存失败，失败原因：${response?.message}`);
        callback();
      }
    },

    // 根据Id编辑理财直融信息
    *editInformationById({ payload, callback }, { call, put }) {
      const response = yield call(api.moneyInformation.editInformationById, payload);
      if (response && response.success) {
        callback(response.data);
      } else {
        message.error(`获取信息失败，失败原因：${response?.message}`);
        callback();
      }
    },

    // 删除接口
    *deleteInformationById({ payload, callback }, { call, put }) {
      const response = yield call(api.moneyInformation.deleteInformationById, payload);
      if (response && response.success) {
        message.success('删除成功');
        callback(true);
      } else {
        message.error(`删除失败，失败原因：${response?.message}`);
        callback();
      }
    },

    // 转换接口
    *transitionInformation({ payload, callback }, { call, put }) {
      const response = yield call(api.moneyInformation.transitionInformation, payload);
      if (response && response.code == '0') {
        message.success('转换成功');
        callback(true);
      } else {
        message.error(`转换失败，失败原因：${response?.message}`);
        callback();
      }
    },

    // 查询评级字典
    *fetchDictByKeys({ payload, callback }, { call, put }) {
      const response = payload.dictData;
      console.log('response', response);
      if (response) {
        const tempArr = [];
        response.BANK_MARKET_SELECT2 &&
          response.BANK_MARKET_SELECT2.map((item) => {
            tempArr.push({ title: item.value, value: item.code });
          });
        const agencyList = [];
        response.COUNTER_PARTY_RATE_ORGAN &&
          response.COUNTER_PARTY_RATE_ORGAN.map((item) => {
            agencyList.push({ title: item.value, value: item.code });
          });
        // 计息规则
        const interestRulesList = [];
        response.ASSETS_INTEREST_BASIS &&
          response.ASSETS_INTEREST_BASIS.length > 0 &&
          response.ASSETS_INTEREST_BASIS.forEach((item) => {
            interestRulesList.push({ title: item.value, value: item.code });
          });
        // 底层融资人融资用途下拉数据
        const financingSelect = [];
        response.UNDERLYING_FINANCIERS_DICT &&
          response.UNDERLYING_FINANCIERS_DICT.length > 0 &&
          response.UNDERLYING_FINANCIERS_DICT.map((item) => {
            financingSelect.push({ title: item.value, value: item.code });
          });
        callback(tempArr, agencyList, interestRulesList, financingSelect);
      } else {
        message.error(`查询数据字典失败，失败原因：`);
        callback();
      }
    },

    // 计算付息
    *calcFx({ payload, callback }, { call, put }) {
      const response = yield call(api.moneyInformation.calcFx, payload);
      if (response && response.code == 0) {
        const data = response.data.dataSource || [];
        data &&
          data.map((item) => {
            if (moment(item.endDate) < moment()) {
              // 到期日小于当前日期 则不可修改
              item.disable = 1;
            }
          });
        data.sort(function (a, b) {
          if (moment(a?.startDate).format('YYYY-MM-DD') == moment(b?.startDate).format('YYYY-MM-DD')) {
            moment(a?.endDate).format('YYYY-MM-DD') > moment(b?.endDate).format('YYYY-MM-DD') ? 1 : -1;
          }
          return moment(a.startDate).format('YYYY-MM-DD') > moment(b.startDate).format('YYYY-MM-DD') ? 1 : -1;
        });
        callback(data);
      } else {
        callback();
        message.error(`付息计算失败，失败原因：${response?.message}`);
        console.error(`付息计算失败，失败原因：${response?.message}`, '接口名称： queryJsFx 接口：nonstandard/queryJsFx');
      }
    },

    reducers: {
      save(state, { payload }) {
        return {
          ...state,
          ...payload,
        };
      },
    },
  },
};
